package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.csm.api.base.enums.DataTypeEnum;
import com.cmc.cloud.cmclink.doc.rpcservice.CustomerRpcService;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BkgTarriffNoErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.vo.bkgtarriffnovo.BkgTarriffNoBaseVO;
import com.cmc.cloud.cmclink.doc.vo.bkgtarriffnovo.BkgTarriffNoCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.bkgtarriffnovo.BkgTarriffNoPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.bkgtarriffnovo.BkgTarriffNoRespVO;
import com.cmc.cloud.cmclink.doc.vo.bkgtarriffnovo.BkgTarriffNoUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import java.util.*;

import com.cmc.cloud.cmclink.doc.service.BkgTarriffNoService;
import com.cmc.cloud.cmclink.doc.entity.BkgTarriffNoDO;
import com.cmc.cloud.cmclink.doc.mapper.BkgTarriffNoMapper;


/**
 * 订舱公布价约号 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class BkgTarriffNoServiceImpl implements BkgTarriffNoService {

    @Resource
    private BkgTarriffNoMapper bkgTarriffNoMapper;

    @Resource
    private CustomerRpcService nameTransferService;

    @Override
    public void createBkgTarriffNo(BkgTarriffNoCreateReqVO createReqVO) {
        // 插入
        validateDocBkConfirmPrintSetupUnique(createReqVO, null);
        BkgTarriffNoDO bkgTarriffNo = BeanUtil.copyProperties(createReqVO, BkgTarriffNoDO.class);
        bkgTarriffNoMapper.insert(bkgTarriffNo);
    }

    @Override
    public void updateBkgTarriffNo(BkgTarriffNoUpdateReqVO updateReqVO) {
        // 校验存在
        validateBkgTarriffNoExists(updateReqVO.getId());
        validateDocBkConfirmPrintSetupUnique(updateReqVO, updateReqVO.getId());
        // 更新
        BkgTarriffNoDO updateObj = BeanUtil.copyProperties(updateReqVO, BkgTarriffNoDO.class);
        bkgTarriffNoMapper.updateById(updateObj);
    }

    @Override
    public void deleteBkgTarriffNo(Long id) {
        // 校验存在
        validateBkgTarriffNoExists(id);
        // 删除
        bkgTarriffNoMapper.deleteById(id);
    }

    private void validateBkgTarriffNoExists(Long id) {
        if (bkgTarriffNoMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(BkgTarriffNoErrorCodeConstants.BKG_TARRIFF_NO_NOT_EXISTS);
        }
    }


    private void validateDocBkConfirmPrintSetupUnique(BkgTarriffNoBaseVO vo, Long id) {
        LambdaQueryWrapperX<BkgTarriffNoDO> queryWrapperX = new LambdaQueryWrapperX<BkgTarriffNoDO>()
                .eq(BkgTarriffNoDO::getBkpCode, vo.getBkpCode());
        if (id != null) {
            queryWrapperX.ne(BkgTarriffNoDO::getId, id);
        }
        if (bkgTarriffNoMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(BkgTarriffNoErrorCodeConstants.BKG_TARRIFF_NO_EXISTED);
        }
    }

    @Override
    public BkgTarriffNoDO getBkgTarriffNo(Long id) {
        return bkgTarriffNoMapper.selectById(id);
    }

    @Override
    public BkgTarriffNoDO getByBkpCode(String bkpCode) {
        return bkgTarriffNoMapper.selectOne(BkgTarriffNoDO::getBkpCode, bkpCode);
    }

    @Override
    public List<BkgTarriffNoDO> getBkgTarriffNoList(Collection<Long> ids) {
        return bkgTarriffNoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BkgTarriffNoRespVO> getBkgTarriffNoPage(BkgTarriffNoPageReqVO reqVO) {
        LambdaQueryWrapperX<BkgTarriffNoDO> queryWrapperX = new LambdaQueryWrapperX<>();
        if (StringUtils.isNotEmpty(reqVO.getKeyWord())) {
            queryWrapperX.like(BkgTarriffNoDO::getBkpCode, reqVO.getKeyWord());
            List<String> strings = nameTransferService.addQuery(reqVO.getKeyWord(), DataTypeEnum.CUSTOMER);
            if (CollectionUtil.isNotEmpty(strings)) {
                queryWrapperX.or().in(BkgTarriffNoDO::getBkpCode, strings);
            }
        }
        PageResult<BkgTarriffNoDO> pageResult = bkgTarriffNoMapper.selectPage(reqVO, queryWrapperX);
        List<BkgTarriffNoRespVO> voList = BeanUtil.copyToList(pageResult.getList(), BkgTarriffNoRespVO.class);
        if (CollectionUtil.isNotEmpty(voList)) {
            nameTransferService.fillName(voList);
        }
        return new PageResult<>(voList, pageResult.getTotal());
    }
}
